package design_patterns;

/**
 * 状态模式
 */
public class StatePattern {
    public static void main(String[] args) {
        MyContext context = new MyContext(new ConcreteStateA());
        context.request();
        context.request();
        context.request();
        context.request();
    }
}

abstract class State {
    public abstract void handle(MyContext context);
}

class MyContext {
    private State state;
    public void request() {
        state.handle(this);
    }

    public MyContext(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }
}

class ConcreteStateA extends State {

    @Override
    public void handle(MyContext context) {
        context.setState(new ConcreteStateB());
    }
}

class ConcreteStateB extends State {

    @Override
    public void handle(MyContext context) {
        context.setState(new ConcreteStateA());
    }
}